package com.jrx.anytxn.transaction.job.customer.step;

import com.jrx.anytxn.common.constant.Constants;
import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.customer.constant.CustomerConstant;
import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.customer.entity.CmCustomerSecondLevel;
import com.jrx.anytxn.customer.service.ICustomerService;
import com.jrx.anytxn.transaction.dto.common.SendSmsReq;
import com.jrx.anytxn.transaction.service.ISmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * @comment: 验证用户身份证有效期Processor
 * @author: liuzb
 * @date: 2022/06/06 15:00
 */
@Service
public class IdCardOverdueProcessor implements ItemProcessor<CmCustomerFirstLevel, CmCustomerFirstLevel> {

    private static final Logger logger = LoggerFactory.getLogger(IdCardOverdueProcessor.class);

    @Autowired
    private ICustomerService iCustomerService;
    @Autowired
    private ISmsService iSmsService;

    @Override
    public CmCustomerFirstLevel process(CmCustomerFirstLevel firstLevel) throws Exception {
        // 到期前一共发短信&微信消息3次（前一个月，前一周，前三天）
        // 尝试更新证件信息（前一个月1次+前一周1次+3天内+已过期的）
        // 当前日期
        Date today = DateUtils.parseDate(new Date());
        // 提前一个月
        Date nextMonthDay = DateUtils.addMonth(today, 1);
        // 提前一周
        Date nextSevenDay = DateUtils.addDay(today, 7);
        // 前三天
        Date nextThreeDay = DateUtils.addDay(today, 3);
        // 身份证有效期
        Date idExpireDate = DateUtils.parseDate(firstLevel.getIdExpireDate());

        // 是否为空
        if (Objects.isNull(today) || Objects.isNull(idExpireDate)) {
            logger.error("用户:{}的today:{}或者idExpireDate:{}日期有为空的情况", firstLevel.getCustomerId(), today, idExpireDate);
            return null;
        }
        // 已过有效期，3天内，第7天，第1个月
        if (today.before(idExpireDate) || nextThreeDay.compareTo(idExpireDate) >= 0 || nextSevenDay.compareTo(idExpireDate) == 0 || nextMonthDay.compareTo(idExpireDate) == 0) {
            logger.info("用户{}身份证有效期在3天内，第7天，第1个月", firstLevel.getCustomerId());
            // 先尝试更新，重新给身份证有效期赋值
            iCustomerService.validIdCard(firstLevel, DateUtils.format(nextMonthDay));
            idExpireDate = DateUtils.parseDate(firstLevel.getIdExpireDate());
            // 是否为空
            if (Objects.isNull(idExpireDate)) {
                logger.error("用户:{}的idExpireDate:{}日期有为空的情况", firstLevel.getCustomerId(), null);
                return null;
            }
            // 再次判断是否符合发送的条件
            if (nextThreeDay.compareTo(idExpireDate) == 0 || nextSevenDay.compareTo(idExpireDate) == 0 || nextMonthDay.compareTo(idExpireDate) == 0) {
                // 前3天内只发送一次,大于3天的且还在有效期内一共发送3次
                if (DateUtils.getDaysBetween(idExpireDate, today) > 2) {
                    logger.info("修改后用户{}身份证有效期在第3天，第7天，第1个月", firstLevel.getCustomerId());
                    String customerId = firstLevel.getCustomerId();
                    SendSmsReq req = new SendSmsReq();
                    CmCustomerSecondLevel customerSecondLevel = iCustomerService.getSecondByChannelAndCustomerIdAndTensnId(Constants.DEFAULT_CHANNEL, customerId, firstLevel.getTenantId(), null);
                    if (null != customerSecondLevel) {
                        logger.info("----证件即将失效期短信组装参数中----");
                        //生成流水号
                        req.setChannel(customerSecondLevel.getChannel());
                        req.setTenantId(customerSecondLevel.getTenantId());
                        // 身份证有效期短信
                        req.setSmsType(CustomerConstant.SMS_TYPE_ID_WILL_INVALID);
                        String seqNo = (int) (Math.random() * 100000) + System.currentTimeMillis() + "";
                        req.setSeqNo(seqNo);
                        String mobile = customerSecondLevel.getMobile();
                        req.setMobile(mobile);
                        req.setCustomerId(customerId);
                        req.setExpireDate(firstLevel.getIdExpireDate());
                        iSmsService.sendSmsMq(req);
                        logger.info("发送成功{}",customerSecondLevel.getCustomerId());
                    }
                }
            }
        }
        return firstLevel;
    }

}
